#include <iostream>
#include <memory>
#include <vector>
using namespace std;
//共享指针
void test(){
    shared_ptr<int> sp(new int(10));
    cout<<sp.get()<<endl;
    cout<<*sp<<endl;
    cout<<sp.use_count()<<endl;
    //使用拷贝构造函数
    shared_ptr<int> sp2=sp;
    cout<<sp2.use_count()<<endl;
    cout<<sp.use_count()<<endl;
    //使用赋值运算符函数
    shared_ptr<int> sp3(new int(20));
    sp3=sp2;
    cout<<sp2.use_count()<<endl;
    cout<<sp3.use_count()<<endl;
}
class Point{
public:
    Point(int x,int y)
    :_x(x)
    ,_y(y)
    {

    }
    void print(){
     //   cout<<_x<<","<<_y<<endl;
            cout<<"nihao"<<endl;
    }
private:
    int _x;
    int _y;
public:
    static Point * pt;
};
Point * Point::pt=nullptr;
//将shared_ptr作为容器vector的元素
void test2(){
    vector<shared_ptr<Point>> vec;
    shared_ptr<Point> sp(new Point(2,0));
    //引用计数加一,因为走的是拷贝构造函数，会额外拷贝一个副本用来传参
    vec.push_back(sp);
    cout<<sp.use_count()<<endl;
    //此时的sp仍然有效，而且引用计数是2
    //引用计数不变，走的是移动拷贝构造函数，不会额外拷贝一个副本用来传参
    vec.push_back(std::move(sp));
    //这里以后的所有sp都不在有实际意义，因为sp的原始指针是空的
    cout<<sp.get()<<endl;
    cout<<sp.use_count()<<endl;
    vec.push_back(sp);
    cout<<sp.use_count()<<endl;
    cout<<"=========="<<endl;
    cout<<vec[0].use_count()<<endl;
    cout<<vec[1].use_count()<<endl;
    cout<<vec[2].use_count()<<endl;
    cout<<vec.size()<<endl;
    for(auto & ele: vec){
        ele->print();
    }
}
//验证一下空指针能否使用类函数
void test3(){
    Point::pt->print();
    Point pt(1,2);
    Point* p=&pt;
    p->print();
    p=nullptr;
}
int main(int argc, char * argv[]){
    test3();    
    return 0;
}

